<?php
namespace app\wxapp\controller;

use app\model\Koi as Koi_mod;
use app\model\Theme; 
use app\model\Poster; 
use app\model\Fans; 
use app\model\Game; 

class Koi extends Base
{
    public function Koi()
    {
/**
 * Created by 易宣软件
 * Desc:五锦拼霸王
 * User:cbw
 * Date: 2020/12/7
 */
global $_P, $_G;
$response = modelShow(40000);
//yxlog($_P);
//yxlog("当前UID:" . $_G['member']['uid']);
if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $op = isset($_P['op'])?$_P['op']:'display'; //display默认   order 查找对应的砍价实例   help 帮砍查询 search_bargain
    $uid = empty($_G['member']['uid'])?0:$_G['member']['uid'];
    $themeid = isset($_P['themeid'])?$_P['themeid']:'';
    $from_uid = isset($_P['fromuid'])?$_P['fromuid']:'';
    //基本检测
    $fans = $this->checkuser();
    $system=$_G['system'];
    $theme = Theme::getThemeById($themeid);
    $config =   Theme::getConfig($themeid);

    if ($op=='display'){
        try {

            // 判断上一级是否为员工，若是则修改fans信息koi_is_form_worker
            $fans = Koi_mod::checkFromTypeNew($fans,$from_uid,$themeid);
            $is_show_lt = Koi_mod::checkTimeInActivity($config); //是否显示五锦组件
            $my_prize_img = $config['koi_my_prize_img']; //是否显示五锦组件
            $koi_kf_img = $config['koi_kf_img']; //是否显示五锦组件
            $is_new_user = Koi_mod::checkNewUser($fans);
            if ($fans['koi_counter']<=0){
//                $check = Koi_mod::checkUserNoRegisterChance($uid,$fans,$config,$themeid,$from_uid);
                $check_from_chance = Koi_mod::checkFromUserAddChance($themeid,$from_uid,$uid,$is_new_user,$config,$theme);
            }
            //定时领取
            $timeset_arr = [
                't_id'=>0,
                'btn_txt'=>'',
                'timeset'=>'',
                'icon'=>'',
                'status5'=>0,
                'config_time'=>$config['koi_timeset'],
            ];
            if ($config['koi_timeset'] && $config['koi_timeset']>0){
                //判断定时领取
                //判断今日是否有定时领取记录
                $has_timeset_flag = Koi_mod::getNoReceiveTypeFlag($uid,$themeid,0);
                if (!$has_timeset_flag || ($has_timeset_flag&&$has_timeset_flag['status']==3)){
                    $insert_data = [
                        'uid'=>$uid,
                        'get_at'=>date('Y-m-d H:i:s', strtotime('+'.$config['koi_timeset'].'hour')),
                        'status'=>1,
                        'themeid'=>$themeid
                    ];
                    Koi_mod::insertTimeSetItem($insert_data);
                }

                //判断当前是否有进行中的记录
                $has_process_receive = Koi_mod::getLastWaitFlag($uid,$themeid,1);
                if ($has_process_receive) {
                    $new_time = time();
                    $get_at = strtotime($has_process_receive['get_at']);
                    if ($get_at<$new_time) {
                        $other_id = $has_process_receive['t_id'];
                        $status5 = 1;
                        $timeset = '';
                        $btn5 = '去领取';
                    } else {
                        $other_id = $has_process_receive['t_id'];
                        $btn5 = '后领取';
                        $status5 = 0;
                        $timeset =$has_process_receive['get_at'];
                    }
                    $timeset_arr['t_id']=$other_id;
                    $timeset_arr['status5']=$status5;
                    $timeset_arr['btn_txt']=$btn5;
                    $timeset_arr['timeset']=$timeset;
                    $timeset_arr['icon']=$config['koi_timeset_icon'];
                }
            }

            //$check_in_five_lt_time = Koi_mod::checkInFiveLtTime($themeid,$uid,$config);
            $is_end = Koi_mod::checkThemeTime($theme);

            $error_txt = Koi_mod::BaseCheck($theme,$config,$system);


            $top_imgs = $config['bannerimgs'];
            $open_five_lt = Koi_mod::checkFiveLtStatus($config,$themeid,$uid);
            $five_lt_tips = $config['koi_five_lt_tips'];
            $theme_rule = $config['koi_rules'];
            $open_king_five_koi = $config['koi_five_king'];
            $koi_convert_tips = $config['koi_convert_tips'];
            $king_five_koi = Koi_mod::formatGetKoiMethodList($themeid,$uid,0,0,0);
            $lucky_star = Koi_mod::checkUserHasLuckyStarChance($themeid,$uid,$config,$from_uid);
            $has_five_lt_chance = Koi_mod::checkHasFiveLtChance($themeid,$uid);
            $grid_koi = Koi_mod::getGridKoiInfo($uid,$from_uid,$themeid);
            $more_amazing_icon = '';

            $need_info = Koi_mod::getNeedInfo($config,$uid,$themeid);
            $koi_to_pintuan_icon = isset($config['koi_to_pintuan_icon'])?$config['koi_to_pintuan_icon']:'https://css2020.yixuan.net/2020css/five_koi/5s.png';
            $pintuan_page_path = '';
            $pintuan_theme = Game::getThemeListByType('lz_game',$themeid);
            if ($pintuan_theme['type_theme_total']>0){
                $new_theme = Theme::getThemeById($pintuan_theme['type_theme_list'][0]['id']);

                $fanstemp = Fans::getworkerid_fromuid($themeid,$fans);
                $workerid = $fanstemp['workerid'];
                $fromuid = $fanstemp['fromuid'];
                $xcxurl = '/pages/index/index?softname=' . MODULE_NAME . '&themeid=' . $new_theme['id']
                    . '&softkey=' . $new_theme['softkey']. '&fromuid=' . $fromuid. '&workerid=' . $workerid;
                $pintuan_page_path= $xcxurl;
                $more_amazing_icon = $config['koi_more_amazing_icon'];
            }
            $is_pay = false;
            $has_order = Koi_mod::checkHasOrder($uid,$config);

            $amazing_show_type = $config['koi_five_more_amazing_type'];

            if ($has_order){
                $is_pay = true;
            }
            if (!$is_pay){
                if ($amazing_show_type){
                    //不显示
                    $more_amazing_icon = '';
                }
            }else{
                $koi_to_pintuan_icon = '';
            }

            $koi_worker_invite_num = $config['koi_worker_invite_num'];

            $friend_help_process = Koi_mod::getUserFriendViewProcess($themeid,$uid,$config);
            $to_max_chance = Koi_mod::checkUserUsedMaxChance($fans,$themeid,$config);
//            幸运星是否收集手机号 逻辑判断
            $koi_lucky_get_mobile = 0;
            if ($lucky_star['has'] && !$fans['mobile'] && $config['koi_lucky_get_mobile']){
                $koi_lucky_get_mobile = 1;
            }
            $format_res = [
                'is_show_lt'=>$is_show_lt,
                'my_prize_img'=>$my_prize_img,
                'koi_kf_img'=>$koi_kf_img,
                'error_txt'=>$error_txt,
                'top_imgs'=>$top_imgs,
                'open_five_lt'=>$open_five_lt,
                'five_lt_tips'=>$five_lt_tips,
                'has_five_lt'=>$has_five_lt_chance,
                'theme_rule'=>$theme_rule,
                'open_king_five_koi'=>$open_king_five_koi,//开启五锦拼霸王
                'king_five_koi'=>$king_five_koi,//五锦拼霸王
                'grid_koi'=>$grid_koi,//锦鲤抽奖
                'need_info'=>$need_info,//后台收集信息数组
                'lucky_star'=>$lucky_star['info'],
                'has_lucky_star'=>$lucky_star['has'],
                'koi_to_pintuan_icon'=>$koi_to_pintuan_icon,
                'pintuan_page_path'=>$pintuan_page_path,
                'is_end'=>$is_end,
                'has_order'=>$has_order,
                'more_amazing_icon'=>$more_amazing_icon,
                'is_pay'=>$is_pay,
                'friend_help_process'=>$friend_help_process,
                'timeset_arr'=>$timeset_arr,
                'koi_worker_invite_num'=>$koi_worker_invite_num,
                'to_max_chance'=>$to_max_chance,
                'koi_convert_tips'=>$koi_convert_tips,
                'koi_convert_content'=>[
                    'koi_convert_content1' => $config['koi_convert_content1'],
                    'koi_convert_content2' => $config['koi_convert_content2'],
                    'koi_convert_content3' => $config['koi_convert_content3']
                ],
                'koi_lucky_get_mobile'=>(int)$koi_lucky_get_mobile,
            ];
        }catch (Exception $e){
            var_dump($e->getMessage());die();
        }
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='my_prize'){
        try {
            $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
            $res = Koi_mod::formatMyPrizeList($themeid,$uid,$config);
            $format_res = [
                'error_txt'=>$error_txt,
                'prize_list'=>$res['prize_list'],
                'convert_list'=>$res['convert_list'],
                'default_package'=>$res['default_package'],
                'chest_flag'=>$res['chest_flag'],
            ];
        }catch (Exception $e){
            var_dump($e->getMessage());die();
        }
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='get_lt'){
        $is_show_lt = Koi_mod::checkTimeInActivity($config); //是否显示五锦组件
        if ($is_show_lt){
            try {
                $p_ids = $_P['p_ids'];
                $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
                $has_lt = false;
                $lt_key = 0;
                $lt_item = [];
                $lt_img = '';
                $lt_title = '';
                $lt_desc = '';
                $btn_txt = '';
                $btm_txt = '';
                $fans = Koi_mod::getUserInfoById($uid,$themeid);
                if (!$error_txt && $fans['koi_counter']>0){
                    if (!$config['koi_five_noun']){
                        //唯一性
                        $p_ids_arr= explode(',',$p_ids);
                        $flag_res = Koi_mod::getKoiFlagList($themeid,$fans['uid'],'default');
                        if ($flag_res){
                            foreach ($flag_res as $k => $val){
                                $used_key = array_search($val['from_type_id'],$p_ids_arr);
                                if ($used_key){
                                    array_splice($p_ids_arr,$used_key,1);
                                }
                            }
                        }
                        $p_ids = implode(",",$p_ids_arr);
                    }
                    $new_p_ids = "(".$p_ids.")";

                    $total_pr = (int)Koi_mod::getKoiPricePrsByIds($themeid,$new_p_ids)['total_pr'];
                    $new_ratiio = 0;
                    $rand_key = rand(1,$total_pr);
                    $list = Koi_mod::getPriceListGetLtByIds($themeid,$new_p_ids,'',0,1);
                    foreach ($list as $k =>$v){
                        $new_ratiio += $v['pr'];
                        if ($k+1 == count($list)){
                            $lt_item = $v;
                            $lt_key = $v['p_id'];
                        }else if ($rand_key<=$new_ratiio){
                            $lt_item = $v;
                            $lt_key = $v['p_id'];
                            break;
                        }
                    }

                    $lose_list = Koi_mod::getKoiPrizeListJoinKoi($themeid,'',1,1);

                    $lose_key = rand(0,count($lose_list)-1);
                    $lose_item = $lose_list[$lose_key];

                    $lose_key = $lose_item['p_id'];



                    $has_flag = Koi_mod::checkUserHasKoiFlagByPrizeId($themeid,$uid,$lt_key);
                    $has_lt = true;
                    $lt_title = '恭喜您抽中'.$lt_item['name'];
                    if ($config['koi_five_king']==1){
                        $lt_desc = $lt_item['l_title'] . ":" . $lt_item['short_name'];
                    }else{
                        $lt_desc = $lt_item['short_name'];
                    }
                    $btn_txt = '继续抽奖';
                    $btm_txt = $config['koi_btm_tips'];

                    $prize_qty = $lt_item['qty'];
                    $new_qty = $prize_qty-1;
                    $where = ['p_id'=>$lt_item['p_id']];
                    if ($new_qty<=0){
                        $data = ['qty'=>0,'status'=>2];
                    }else{
                        $data = ['qty'=>$new_qty];
                    }

                    $update_item = Koi_mod::editKoiPrizeItemByWhere($where,$data);

                    $new_list = Koi_mod::getKoiPrizeListJoinKoi($themeid,'',0,1);
                    foreach ($new_list as $kk => $vv){
                        if ($lt_item['p_id']==$vv['p_id']){
                            $lt_key = $kk;
                            break;
                        }
                    }
                    //todo 判断中奖的必中奖
//                if ($has_flag>0){
//                    $has_lt = true;//新曾判断逻辑
//                    $lt_item = $lose_item;
//                    $lt_key = 1;
//                    $lt_title = '恭喜您抽中'.$lt_item['l_title'];
//                    $lt_desc = $lt_item['name'];
//                    $btn_txt = '继续抽奖';
//                    $btm_txt = $config['koi_btm_tips'];
//                    $new_list = Koi_mod::getKoiPrizeListJoinKoi($themeid,'',1,1);
//                    foreach ($new_list as $kk => $vv){
//                        if ($lt_item['p_id']==$vv['p_id']){
//                            $lt_key = $kk;
//                            break;
//                        }
//                    }
//                }else{
//
//                }
                    $new_chance_num = $fans['koi_counter']-1;
                    $update_userinfo = Koi_mod::updateUserInfo($uid,['koi_counter'=>$new_chance_num]);
                    $insert_chance_data = [
                        'uid'=>$uid,
                        'type'=>'system',
                        'chance'=>-1,
                        'themeid'=>$themeid
                    ];
                    $insert_chance_res = Koi_mod::insertKoiChanceItem($insert_chance_data);
                    $insert_flag_data = [
                        'uid'=>$uid,
                        'from_type'=>'default',
                        'from_type_id'=>$lt_item['p_id'],
                        'used_koi_id'=>$lt_item['l_id'],
                        'status'=>1,
                        'themeid'=>$themeid
                    ];
                    $insert_flag_res = Koi_mod::insertKoiFlagItem($insert_flag_data);
                }
                $grid_koi = Koi_mod::getGridKoiInfo($uid,$from_uid,$themeid);

                $king_five_koi = Koi_mod::formatGetKoiMethodList($themeid,$uid,0,0,0);
                unset($lt_item['pr']);
                unset($lt_item['show_sort']);
                unset($lt_item['qty']);
                unset($lt_item['status']);
                unset($lt_item['is_lose']);
                $format_res = [
                    'error_txt'=>$error_txt,
                    'has_lt'=>$has_lt,
                    'lt_item'=>$lt_item,
                    'lt_key'=>$lt_key,
                    'lt_img'=>$lt_item['small_img'],
                    'lt_title'=>$lt_title,
                    'lt_name'=>$lt_desc, //$lt_item['name'],
                    'lt_desc'=>$lt_desc,
                    'koi_view'=>(int)$config['koi_view'],
                    'btn_txt'=>$btn_txt,
                    'btm_txt'=>$btm_txt,
                    'grid_koi'=>$grid_koi,
                    'king_five_koi'=>$king_five_koi
                ];
            }catch (Exception $e){
                var_dump($e->getMessage());die();
            }
            $response = modelShow(200,'',$format_res);
        }

    }elseif ($op=='koi_detail'){
        $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
        $koi_content = htmlspecialchars_decode($config['koi_content']);
        $koi_detail = Poster::getContentvideoimg($config,$koi_content);
        $format_res = [
            'error_txt'=>$error_txt,
            'koi_detail'=>$koi_detail
        ];
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='my_koi'){
        $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
        $king_five_koi = Koi_mod::formatGetKoiMethodList($themeid,$uid,0,0,0);
        $list = $king_five_koi['list'];
        $method_list = $king_five_koi['method_list'];
        $format_res = [
            'error_txt'=>$error_txt,
            'list'=>$list,
            'method_list'=>$method_list
        ];
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='bind_king'){
        $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
        $m_id = $_P['m_id'];
        $res = Koi_mod::checkAndBindKing($m_id,$themeid,$uid);
        $format_res = [
            'error_txt'=>$error_txt,
            'result'=>true
        ];
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='sub_msg'){
        $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
        $sub_msg = false;
        $data = [
            'uid'=>$uid,
            'tmp_id'=>'xhtqfQ0a7GHKYBLydc7fMC4vlsu9cLzd6oWWij6vnG0',
            'page'=>'',
            'status'=>1,
            'themeid'=>$themeid
        ];
        $res = Koi_mod::insertKoiSubMsg($data);
        if ($res){
            $sub_msg = true;
        }
        $format_res = [
            'error_txt'=>$error_txt,
            'sub_msg'=>$sub_msg
        ];
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='convert_chance'){
        $res = false;
        //兑换定时抽奖机会
        $error_txt = Koi_mod::BaseCheck($theme,$config,$system);
        $t_id = $_P['t_id'];
        $where = ['t_id'=>$t_id,'uid'=>$fans['uid'],'themeid'=>$themeid];
        $get_item = Koi_mod::getTimeSetFlagItem($where);
        if ($get_item && $get_item['status']==1) {
            $where = ['t_id' => $get_item['t_id']];
            $data = ['status' => 3];
            Koi_mod::editTimeSetItem($where,$data);
            $add_chance_res = Koi_mod::editUserKoiCounter('add',1,'timeset',$uid,$uid,$fans,$config,$themeid,$theme);
            $res = true;
        }
        $format_res = [
            'error_txt'=>$error_txt,
            'res'=>$res
        ];
        $response = modelShow(200,'',$format_res);
    }
}
wxapp_result(0, '',$response);
}
}